$(function () {
    //常用jQuery对象
    var class_datagrid = $("#class_list");
    var class_dailog = $("#class_dialog");
    var class_form = $("#class_form");

    //==========================================================================
    //封装方法
    var methodObj = {
        //添加按钮方法
        add: function () {
            class_form.form("clear");
            class_dailog.dialog("open");
            class_dailog.dialog("setTitle", "新增");
        },

        //编辑按钮方法

        //编辑按钮方法
        edit: function () {
            class_form.form("clear");
            var row = class_datagrid.datagrid("getSelected");
            if (!row) {
                $.messager.alert("温馨提示", "亲!请至少选择一条数据", "info");
                return;
            }
            class_dailog.dialog("open");
            class_dailog.dialog("setTitle", "编辑");
            //回显所在学院
            if (row.collegeName) {
                row["collegeName.id"] = row.collegeName.id;
            }
            //回显教师
            if (row.classroom) {
                row["classroom.id"] = row.classroom.id;
            }
            //回显班主任
            if (row.classManager) {
                row["classManager.id"] = row.classManager.id;
            }
            //任课教师
            console.log(row);
            if (row.teacher) {
                row["teacher.id"] = row.teacher.id;
            }
            class_dailog.form("load", row);


        },


        //编辑或新增方法具体逻辑
        saveOrUpdate: function () {
            class_form.form("submit", {
                url: "/class/saveOrUpdate.do",
                success: function (data) {
                    data = $.parseJSON(data);
                    if (data.success) {
                        $.messager.alert("温馨提示", data.msg, "info", function () {
                            class_dailog.dialog("close");
                            class_datagrid.datagrid("reload");
                        });
                    } else {
                        $.messager.alert("温馨提示", data.msg, "info", function () {
                            class_dailog.dialog("close");
                        });
                    }
                }
            })
        },

        //删除班级按钮方法
        btn_remove: function () {
            var row = class_datagrid.datagrid("getSelected");
            if (!row) {
                $.messager.alert("温馨提示", "亲!请至少选择一条数据", "info");
                return;
            }
            $.messager.confirm('温馨提示',"请问确定要删除此班级吗?", function (r) {
                if (r) {
                    $.get("/class/delete.do", {id: row.id}, function (data) {
                        if (data.success) {
                            $.messager.alert("温馨提示", data.msg, "info", function () {
                                class_datagrid.datagrid("reload");
                            });
                        } else {
                            $.messager.alert("温馨提示", data.msg, "info");
                        }
                    }, "json")
                }
            });
        },


        //设置班级状态
        changeState: function () {
            var row = class_datagrid.datagrid("getSelected");
            if (!row) {
                $.messager.alert("温馨提示", "亲!请至少选择一条数据", "info");
                return;
            }
            var class_state = "亲,您确定要设置为已开班吗？";
            if (row.state) {
                var class_state = "亲,您确定要设置为未开班吗？";
            }
            $.messager.confirm('温馨提示', class_state, function (r) {
                if (r) {
                    $.get("/class/changState.do", {id: row.id}, function (data) {
                        if (data.success) {
                            $.messager.alert("温馨提示", data.msg, "info", function () {
                                class_datagrid.datagrid("reload");
                            });
                        } else {
                            $.messager.alert("温馨提示", data.msg, "info");
                        }
                    }, "json")
                }
            });

        },

        //刷新页面
        reloadList: function () {
            class_datagrid.datagrid("reload");
        },
        //导出文件模板
        getImportXlsMode:function () {
            window.open("/lesson/getImportXlsMode.do");
        },

        //上传班级课表excel表格
        importXls: function () {
            //判断一下表格是否有东西
            var row = class_datagrid.datagrid("getSelected");
            if (!row) {
                $.messager.alert("温馨提示", "亲!请至少选择一条数据", "info");
                return;
            }
            /*上传课件，直接根据点击事件发送ajax后台将该行的数据显示出来*/
            $.get("/lesson/getClassById.do",{clzId:row.id});
            $("#importXls_form").form("submit", {
                url: "/lesson/importXls.do",
                success: function (data) {
                    data = $.parseJSON(data);
                    if (data.success) {
                        $.messager.alert("温馨提示", data.msg, "info",function () {
                            class_datagrid.datagrid("reload");
                        });
                    } else {
                        $.messager.alert("温馨提示", data.msg, "info");
                    }
                }
            })


        }
    }


    //==========================================================================================
    //按钮绑定事件
    $("[data-cmd]").click(function () {
        var cmd = $(this).data("cmd");
        methodObj[cmd]();
    });

    //页面数据加载
    class_datagrid.datagrid({
        fit: true,
        fitColumns: true,
        url: "/class/query.do",
        singleSelect: "true",
        pagination: true,
        rownumbers: true,
        onClickRow: function (index, row) {

            if (row.state) {
                $("#btn_changeState").linkbutton({
                    text: "停用班级"
                });
            } else {
                $("#btn_changeState").linkbutton({
                    text: "启用班级"
                });

            }
        },
        columns: [[
            {field: 'name', title: '班级名称', width: 100, align: "center"},
            {field: 'studentNumber', title: '学员数量', width: 100, align: "center"},
            {
                field: 'collegeName', title: '所在学院', width: 100, align: "center", formatter: function (collegeName) {
                    return collegeName ? collegeName.name : "<font color='red'>未分配学院</font>";
                }
            },
            {
                field: 'state', title: '班级状态', width: 100, align: "center", formatter: function (state) {
                    return state ? "已开班" : "<font color='red'>未开班</font>";
                }
            },
            {
                field: 'classroom', title: '所在教室', width: 100, align: "center",  formatter: function (classroom) {
                    return classroom ? classroom.name : "<font color='red'>未分配教室</font>";
                }
            },
            {
                field: 'classManager', title: '班主任', width: 100, align: "center", formatter: function (classManager) {
                    return classManager ? classManager.username : "<font color='red'>未分配班主任</font>";
                }
            },
            {
                field: 'teacher', title: '任课老师', width: 100, align: "center", formatter: function (teacher) {
                    return teacher ? teacher.username : "<font color='red'>未分配任课老师</font>";
                }
            }
        ]],
        toolbar: "#tools"
    });

    //编辑添加弹出框设置
    class_dailog.dialog({
        width: 350,
        height: 400,
        closed: true,
        buttons: [{
            text: '保存',
            iconCls: "icon-save",
            handler: methodObj["saveOrUpdate"]
        }, {
            iconCls: "icon-cancel",
            text: '取消',
            handler: function () {
                class_dailog.dialog("close");
            }
        }]
    });

    //form表单设置
    class_dailog.form({
        fit: true
    });

});

